<?php class Mod_clinic extends CI_Model {
	
	function __construct()
	    {
		parent::__construct();
	    }

            /*retourner le pays du clinique*/
            function getPaysClinic($id_pays_cli){
                $sql = "SELECT * FROM pays WHERE ID_PAYS = $id_pays_cli";			
                    $Q = $this->db->query($sql);
                    if ($Q->num_rows()>0){
                            foreach ($Q->result_array() as $row){
                                    $pays=$row['PAYS_FR'];
                            }
                    }
                    $Q->free_result();
                    return $pays;

            }
            /*retourner la ville du clinique*/
            function getVilleClinic($id_ville_cli){
                $sql = "SELECT * FROM ville WHERE ID_VILLE = $id_ville_cli";
                $ville='';
                    $Q = $this->db->query($sql);
                    if ($Q->num_rows()>0){
                            foreach ($Q->result_array() as $row){
                                    $ville=$row['NOM_VILLE'];
                            }
                    }
                    $Q->free_result();
                    return $ville;

            }
            
            function getPhotoClinic($id_photo_cli){
                $sql = "SELECT * FROM photo WHERE ID_PHOTO = $id_photo_cli";			
                    $Q = $this->db->query($sql);
                    if ($Q->num_rows()>0){
                            foreach ($Q->result_array() as $row){
                                    $logo=$row['SRC'];
                            }
                    }
                    $Q->free_result();
                    return $logo;

            }
            
        /*ajout compte clinique*/
        function addCompteClinic($data){
            $this->db->insert('compte_clinique',$data);
        }
        /*retourner id compte avec le mail*/
        function getIdCompteByMail($email){
            $sql = "SELECT * FROM compte_clinique WHERE EMAIL =?";			
		$Q = $this->db->query($sql,array($email));
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$id=$row['ID_COMPTE_CLI'];
			}
		}
		$Q->free_result();
		return $id;
	}
        /*ajouter informations cliniques*/
        function addInfoClinic($data){
            $this->db->insert('clinique',$data);
        }
        /*retourner identifiant compte clinique*/
        function verifCompteClinic($email,$mdp) {
		$sql = "SELECT * FROM compte_clinique WHERE email = '$email' AND mdp = '$mdp'";
		$Q = $this->db->query($sql);		
		$id = 0;
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$id = $row['ID_COMPTE_CLI'];
			}
		}
		$Q->free_result();
		return $id;
        }
        /*retournet identifiant clinique*/
        function getClinicByIdCompte($id_compte_cli) {
		$sql = "SELECT * FROM clinique WHERE ID_COMPTE_CLI = '$id_compte_cli'";
		$Q = $this->db->query($sql);		
		$id_cli = 0;
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$id_cli = $row['ID_CLI'];
			}
		}
		$Q->free_result();
		return $id_cli;
        }
        /*retourner nombre de commentaire clinique*/
        function getNbComment($id_cli){
            $sql = "SELECT * FROM poster WHERE ID_CLI = '$id_cli'";
		$Q = $this->db->query($sql);		
		$nb_com = 0;
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$nb_com++;
			}
                }
		$Q->free_result();
		return $nb_com;
        }
        /*retourner identifiant dernier commataire du clinique*/
        function getIdLastComment($id_cli){
            $sql = "SELECT * FROM poster WHERE ID_CLI = '$id_cli' ORDER BY DATE_POST DESC LIMIT 1";
		$Q = $this->db->query($sql);		
		$id_com = 0;
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$id_com = $row['ID_COM'];
			}
		}
		$Q->free_result();
		return $id_com;
        }
        /*retourner dernier commentaire de la clinique par l'identifiant*/
        function getLastComment($id_com){
            $sql = "SELECT * FROM commentaire WHERE ID_COM = '$id_com' AND ETAT_COM=1";
		$Q = $this->db->query($sql);		
		$com = '';
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$com = $row['CONTENU_COM'];
			}
		}
		$Q->free_result();
		return $com;
        }
        /*retourner toutes les informations concernants la clinique*/
        function getAllClinicInfo($id_clinic){
            $sql = "SELECT * FROM clinique WHERE ID_CLI='$id_clinic'";
		$Q = $this->db->query($sql);		
		$data=array();
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[] = $row;
			}
		}
		$Q->free_result();
		return $data;
        }
        /*mettre à jour tous les informations cliniques*/
        function updateAllClinicInfo($data){
	    $this->db->where('ID_CLI',$data['ID_CLI']);
	    $this->db->update('clinique', $data);
        }
        /*retourner mail clinique avec l'identifiant compte clinique*/
        function getEmailClinic($id_compte){
            $sql = "SELECT EMAIL FROM compte_clinique WHERE ID_COMPTE_CLI='$id_compte'";
            $email='';
            $Q = $this->db->query($sql);		
            if ($Q->num_rows()>0){
                    foreach ($Q->result_array() as $row){
                            $email = $row['EMAIL'];
                    }
            }
            $Q->free_result();
            return $email;
            
        }
        
        
        /*retourner mot de passe clinique avec id_compte*/
        function getMdpClinic($id_compte){
            $sql = "SELECT MDP FROM compte_clinique WHERE ID_COMPTE_CLI='$id_compte'";
		$Q = $this->db->query($sql);		
		$mdp='';
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$mdp = $row['MDP'];
			}
		}
		$Q->free_result();
		return $mdp;
            
        }
        /*retourner une liste de pays*/
        function getAllPays(){
                $sql = "SELECT * FROM pays ORDER BY PAYS_FR ASC";
                $list_pays=array();
                $Q = $this->db->query($sql);
                if ($Q->num_rows()>0){
                        foreach ($Q->result_array() as $row){
                                $list_pays[]=$row;
                        }
                }
                $Q->free_result();
                return $list_pays;

            }
            /*retourner liste ville suivant le pays*/
            function getAllVilleFromPays($id_pays){
		$sql = "SELECT * FROM ville WHERE ID_PAYS = '$id_pays' ORDER BY NOM_VILLE ASC";
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$data[]=$row;
			}
		}
		$Q->free_result();
		return $data;
	}
        /*retourner liste ville suivant le pays avec JSON*/
        function getAllVilleJson($id_pays) {
		$json = array();
		$data = array();
		$sql = "SELECT * FROM ville where ID_PAYS = '$id_pays' ORDER BY NOM_VILLE ASC";	
		//echo $sql;
		$Q = $this->db->query($sql);
		if ($Q->num_rows()>0){
			foreach ($Q->result_array() as $row){
				$json[$row['ID_VILLE']][] = $row['NOM_VILLE'];
			}
			$json = json_encode($json);
			
		}
		$Q->free_result();
		header('Content-Type: application/json');
		echo $json;
	}
        
        
}
?>